import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class HashTableApplet extends JApplet
{
	private MyHashTable<String, String> hash;
	
	TextField key;
	TextField value;
	String key_str, value_str;
	Panel input;
	HashPanel graphics;
	JButton addBtn, findBtn,delBtn,resetBtn;
	Label findLabel;
	btnAction ba;
	public HashTableApplet()
	{
		hash = new MyHashTable<String, String>(5);
		//String k = "enter a key";
	    //String v = "enter a value";
	    key       = new TextField();
	    value     = new TextField();
	    addBtn    = new JButton("add");
	    findBtn   = new JButton("find");
	    delBtn    = new JButton("delete");
	    resetBtn  = new JButton("reset");
	    input     = new Panel();
	    graphics  = new HashPanel(hash);
	    findLabel = new Label("found key status:");
	    ba        = new btnAction();
	    
	    addBtn.addActionListener(ba);
	    findBtn.addActionListener(ba);
	    delBtn.addActionListener(ba);
	    resetBtn.addActionListener(ba);
	    
	    input.setLayout(new GridLayout(0,8));
	    input.add(new Label("Key: "));
	    input.add(key);
	    input.add(new Label(" Value: "));
	    input.add(value);
	    input.add(addBtn);
	    input.add(findBtn);
	    input.add(delBtn);
	    input.add(resetBtn);
	    setLayout(new BorderLayout());       // methods of the frame
	    add("North",input);       
	    add("Center",graphics);
	    add("South",findLabel);
	    /*hash.put("red","45");
	    hash.put("der","46");
	    hash.put("grape","48");
	    hash.put("blue","47");
	    hash.put("grule","50");*/
	   
	}
	public void paint (Graphics g)
	{
	    super.paint(g);
	}
 
	
	public boolean action(Event e, Object arg) {
		key_str   = key.getText();
		value_str = value.getText();
		hash.put(key_str, value_str);
	    repaint();
	    return true;
	  }
	
	private class btnAction implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			 String command = ((JButton) arg0.getSource()).getActionCommand();
			 if(command.equals("add"))
			 {
				 key_str   = key.getText();
				 value_str = value.getText();
				hash.put(key_str, value_str);
				repaint();
			 }
			 else if(command.equals("delete"))
			 {
				 key_str   = key.getText();
				 value_str = value.getText();
				 hash.delNode(key_str,value_str);
				 repaint();
			 }
			 else if(command.equals("reset"))
			 {
				 hash = new MyHashTable<String, String>(5);
				 graphics.setHash(hash);
				 repaint();
			 }
			 else
			 {
				 key_str   = key.getText();
				 boolean isFound = hash.containsKey(key_str);
				 if(isFound) 
				 {
					 findLabel.setBackground(Color.black);
					 findLabel.setForeground(Color.green);
				 }
				 else
				 {
					 findLabel.setBackground(Color.white);
					 findLabel.setForeground(Color.red);
				 }
				 findLabel.setText("found key status: "+isFound);
			 }
			
			
		}
		
	}

}
